/*数据库操作*/
/*阿里云短信*/
const Core = require('@alicloud/pop-core');

let signName1 = "领优惠";
let registerTemplateCode = "SMS_180357674"; // 注册
let modifyTemplateCode = "SMS_180347793"; // 修改
let currencyTemplateCode = "SMS_180357676"; //通用(登陆)

var client = new Core({
    accessKeyId: 'LTAI4FmKaDDQqghP3BwVtbHa',
    accessKeySecret: 'j4wUzopI3hmWzHiYwiLxo3LIHboxoZ',
    endpoint: 'https://dysmsapi.aliyuncs.com',
    apiVersion: '2017-05-25'
  });
  
/**
 * 获取验证码
 * @author jaosnZhang
 * @param {Object} ctx 响应主体文本
 * @param {Function} next 异步函数跳返
 * @return {Object} ctx.body 返回信息对象
 */
exports.sendSms = async (ctx, next) => {
    /*参数声明*/
    let query = ctx.request.body;

    let templateCode = ''; 
    switch (query.purpose) {
        case 'register':
            templateCode = registerTemplateCode
            break;
        case 'modify':
            templateCode = modifyTemplateCode
            break;
        default:
            templateCode = currencyTemplateCode
            break;
    }

    var params = {
        "RegionId": "cn-hangzhou",
        "PhoneNumbers": query.phone,
        "SignName": signName1,
        "TemplateCode": templateCode,
        "TemplateParam": "{code: '7412'}"
      }
      
      var requestOption = {
        method: 'POST'
      };
      
      console.log('params',params)
      let result = await client.request('SendSms', params, requestOption);
      return ctx.body = JSON.stringify(result);
}


/**
 * 数据通讯测试
 * @author jaosnZhang
 * @param {Object} ctx 响应主体文本
 * @param {Function} next 异步函数跳返
 * @return {Object} ctx.body 返回信息对象
 */
exports.dataTest = async (ctx, next) => {
    return ctx.body = ctx;
}
